﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController, IUserAction {
    ShoreCtrl leftShoreController, rightShoreController;
    BoatCtrl boatController;
    RoleCtrl[] roleControllers;
    MoveCtrl moveController;
    Judge judge;

    bool isRunning;
    float time;

    public void LoadResources() {
        roleControllers = new RoleCtrl[6];
        for (int i = 0; i < 6; ++i) {
            roleControllers[i] = new RoleCtrl();
            roleControllers[i].CreateRole(Position.role_shore[i], i < 3 ? true : false, i);
        }

        leftShoreController = new ShoreCtrl();
        leftShoreController.CreateShore(Position.left_shore);
        leftShoreController.GetShore().shore.name = "left_shore";
        rightShoreController = new ShoreCtrl();
        rightShoreController.CreateShore(Position.right_shore);
        rightShoreController.GetShore().shore.name = "right_shore";

        foreach (RoleCtrl roleController in roleControllers)
        {
            roleController.GetRoleModel().role.transform.localPosition = leftShoreController.AddRole(roleController.GetRoleModel());
        }

        boatController = new BoatCtrl();
        boatController.CreateBoat(Position.left_boat);

        moveController = new MoveCtrl();

        judge = new Judge();

        isRunning = true;
        time = 60;
    }

    public void MoveBoat() {
        if (isRunning == false || moveController.GetIsMoving()) return;
        if (boatController.GetBoatModel().isRight) {
            moveController.SetMove(Position.left_boat, boatController.GetBoatModel().boat);
        }
        else {
            moveController.SetMove(Position.right_boat, boatController.GetBoatModel().boat);
        }
        boatController.GetBoatModel().isRight = !boatController.GetBoatModel().isRight;
    }

    public void MoveRole(Role roleModel) {
        if (isRunning == false || moveController.GetIsMoving()) return;
        if (roleModel.inBoat) {
            if (boatController.GetBoatModel().isRight) {
                moveController.SetMove(rightShoreController.AddRole(roleModel), roleModel.role);
            }
            else {
                moveController.SetMove(leftShoreController.AddRole(roleModel), roleModel.role);
            }
            roleModel.onRight = boatController.GetBoatModel().isRight;
            boatController.RemoveRole(roleModel);
        }
        else {
            Boat boatModel = boatController.GetBoatModel();
            if (boatModel.isRight == roleModel.onRight && boatModel.priestCount + boatModel.devilCount < 2) {
                if (roleModel.onRight) {
                    rightShoreController.RemoveRole(roleModel);
                }
                else {
                    leftShoreController.RemoveRole(roleModel);
                }
                moveController.SetMove(boatController.AddRole(roleModel), roleModel.role);
            }
        }
    }

<<<<<<< HEAD
    public void Restart()
    {
        moveController.SetMove(Position.left_boat, boatController.GetBoatModel().boat);
        boatController.GetBoatModel().Start();
        leftShoreController.GetShore().Start(false);
        rightShoreController.GetShore().Start(true);
        for (int i = 0; i < 6; ++i)
=======
    public void Restart()
    {
        moveController.SetMove(Position.left_boat, boatController.GetBoatModel().boat);
        boatController.GetBoatModel().Start();
        leftShoreController.GetShore().Start(false);
        rightShoreController.GetShore().Start(true);
        for (int i = 0; i < 6; ++i)
>>>>>>> af278523501920b14a6298f03aa4bd9ad9d33c2d
        {
            roleControllers[i].GetRoleModel().role.transform.parent = leftShoreController.GetShore().shore.transform;
            moveController.SetMove(Position.role_shore[i], roleControllers[i].GetRoleModel().role);
            roleControllers[i].GetRoleModel().Start();
<<<<<<< HEAD
        }
        time = 60;
        isRunning = true;
=======
        }
        time = 60;
        isRunning = true;
>>>>>>> af278523501920b14a6298f03aa4bd9ad9d33c2d
    }

    public void Check() {
        if (isRunning == false) return;
        int state = judge.CheckGame(leftShoreController, rightShoreController, boatController);
        if (state == 1) {
            this.gameObject.GetComponent<UserGUI>().gameMessage = "You Win!";
            isRunning = false;
        }
        else if (state == 2) {
            this.gameObject.GetComponent<UserGUI>().gameMessage = "Game Over!";
            isRunning = false;
        }
        else
            this.gameObject.GetComponent<UserGUI>().gameMessage = "";
    }

    void Awake() {
        SSDirector.GetInstance().CurrentSceneController = this;
        LoadResources();
        this.gameObject.AddComponent<UserGUI>();
    }

    void Update() {
        if (isRunning) {
            time -= Time.deltaTime;
            this.gameObject.GetComponent<UserGUI>().time = (int)time;
            if (time <= 0) {
                this.gameObject.GetComponent<UserGUI>().time = 0;
                this.gameObject.GetComponent<UserGUI>().gameMessage = "Game Over!";
                isRunning = false;
            }
        }
    }
}
